package com.neulogistics.contrib.procuredcenter.api.application.converter;

import cn.hutool.json.JSONUtil;
import com.neulogistics.contrib.procuredcenter.api.application.viewmodel.PurchaseOrderViewModel;
import com.neulogistics.contrib.procuredcenter.domain.model.enumeration.PurchaseType;
import com.neulogistics.contrib.procuredcenter.domain.model.valueobject.PurchaseItem;
import com.neulogistics.contrib.procuredcenter.domain.model.valueobject.VendorInfo;
import com.neulogistics.contrib.procuredcenter.infrastructure.persistence.orm.po.PurchaseOrderPO;
import org.springframework.beans.BeanUtils;

public class PurchaseOrderPOVMConverter {
    private static final int Rate = 100;

    public static PurchaseOrderViewModel POToVM(PurchaseOrderPO purchaseOrderPO){
        if (purchaseOrderPO == null)
            return null;

        PurchaseOrderViewModel purchaseOrderViewModel = new PurchaseOrderViewModel();
        BeanUtils.copyProperties(purchaseOrderPO, purchaseOrderViewModel);
        // long转string
        purchaseOrderViewModel.setPurchaseOrderId(purchaseOrderPO.getPurchaseOrderId().toString());
        // json对象转换
        purchaseOrderViewModel.setPurchaseItem(JSONUtil.toBean(purchaseOrderPO.getPurchaseItem(), PurchaseItem.class));
        // 枚举值转换
        purchaseOrderViewModel.setPurchaseType(PurchaseType.fromValue(purchaseOrderPO.getPurchaseType(), PurchaseType.class));
        // 价格转换
        purchaseOrderViewModel.setPurchaseTotal(Double.valueOf(purchaseOrderPO.getPurchaseTotal()) / Rate);

        return purchaseOrderViewModel;
    }
}
